package com.practice.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.practice.common.result.PageResult;
import com.practice.common.result.Result;
import com.practice.mapper.admin.AdminMenuMapper;
import com.practice.mapper.admin.AdminRoleMapper;
import com.practice.pojo.admin.dto.AdminRoleDTO;
import com.practice.mapper.admin.RoleMenuMapper;
import com.practice.pojo.admin.dto.AdminRolePageDTO;
import com.practice.pojo.admin.dto.RoleDTO;
import com.practice.pojo.admin.entity.AdminChildrens;
import com.practice.pojo.admin.entity.Role;
import com.practice.pojo.admin.vo.AdminRoleVO;
import com.practice.pojo.admin.entity.RoleMenu;
import com.practice.pojo.admin.vo.AdminRoleVO;
import com.practice.service.admin.AdminRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import java.time.LocalDateTime;


@Service
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper,Role> implements AdminRoleService{

    @Autowired
    private AdminRoleMapper adminRoleMapper;
    @Autowired
    private AdminMenuMapper adminMenuMapper;
    @Override
    public Result page(AdminRolePageDTO dto) {
        IPage<Role> page=new Page<>(dto.getPage(),dto.getLimit());

        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Role::getName,dto.getName());
        adminRoleMapper.selectPage(page,lambdaQueryWrapper);

        return Result.success(new PageResult(
                page.getTotal(),
                page.getPages(),
                page.getSize(),
                page.getCurrent(),
                page.getRecords()
        ));
    }

    @Override
    public void delete(String id) {
        adminRoleMapper.deleteById(id);
    }

    @Override
    public Result add(AdminRoleDTO adminRoleDTO) {
        Role role = new Role();
        BeanUtils.copyProperties(adminRoleDTO,role);
        role.setCreateDate(LocalDateTime.now());
        role.setUpdateDate(LocalDateTime.now());
        //添加role进sys_role_menu表,设置权限
        adminRoleMapper.insert(role);

        for (Integer menuid : adminRoleDTO.getMenuids()) {
            RoleMenu rm=new RoleMenu();
            rm.setRoleId(role.getId());
            rm.setMenuId(menuid);
            roleMenuMapper.insert(rm);
        }

        return Result.success();
    }


//    @Override
//    public Result rowInfo(Integer id) {
//
//        List<AdminChildrenVO> adminChildrenVOList = adminMenuMapper.showAll(id);
//
//        for (AdminChildrenVO adminChildrenVO : adminChildrenVOList) {
//            adminChildrenVO.setChildren(rowInfo());
//        }
//
//
//        return null;
//    }
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public Result rowInfo(String id) {
        //得到所有pid为0的，递归把子给它
        List<AdminChildrens> sonId = roleMenuMapper.querySon(id,0L);
        List<AdminChildrens> son = new ArrayList<>(sonId.size());

        for (int i = 0; i < sonId.size(); i++) {
            son.add(getSon(id, sonId.get(i).getId()) );
        }
        Role role = adminRoleMapper.selectById(id);
        AdminRoleVO adminRoleVO = BeanUtil.copyProperties(role, AdminRoleVO.class);
        adminRoleVO.setAuthList(son);
        return Result.success(adminRoleVO);
    }

    public AdminChildrens getSon(String roleId,Long id){
        AdminChildrens adminChildrens = roleMenuMapper.queryCurrent(id);
        adminChildrens.setChildren(CollUtil.newArrayList());

        List<AdminChildrens> sonId = roleMenuMapper.querySon(roleId,id);
        if (sonId == null || sonId.size() == 0){
            AdminChildrens current = roleMenuMapper.queryCurrent(id);
            return current;
        }
        //递归子id
        for (AdminChildrens adminChildren : sonId) {
            adminChildrens.getChildren().add(getSon(roleId,adminChildren.getId()));
        }

        return adminChildrens;
    }

    @Override
    public void update(RoleDTO dto) {
        roleMenuMapper.delete(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId,dto.getId()));
        Role role = BeanUtil.copyProperties(dto, Role.class);
        adminRoleMapper.updateById(role);
        for (Integer menuid : dto.getMenuids()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(dto.getId());
            roleMenu.setMenuId(menuid);
            roleMenuMapper.insert(roleMenu);
        }
    }

    @Override
    public Result allData() {
       /* List<AdminChildrens> sonId = roleMenuMapper.querySon(null,0L);
        List<AdminChildrens> son = new ArrayList<>();

        for (int i = 0; i < sonId.size(); i++) {
            son.add(getSon(null, sonId.get(i).getId()) );
        }
        return Result.success(son);*/

        return Result.success(adminRoleMapper.selectList(null));
    }
}
